/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Grafik;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import root.Controler;

/**
 *
 * @author edgarjulca und fana
 */
public class GrafikTriangle extends GrafikObject {

    private GrafikPoint a;
    private GrafikPoint b;
    private GrafikPoint c;
    private int windowWith = 0;
    private int windowHeight = 0;
    private int offset = 10;

    public GrafikTriangle(int x1, int y1, int x2, int y2, int x3, int y3){
        this.a = new GrafikPoint(x1, y1);
        this.b = new GrafikPoint(x2, y2);
        this.c = new GrafikPoint(x3, y3);
    }
    //setter

    public void setA(GrafikPoint a){
        this.a = a;
    }

    public void setB(GrafikPoint b){
        this.b = b;
    }

    public void setC(GrafikPoint c){
        this.c = c;
    }

    public void setWindowWith(int windowWith){
        this.windowWith = windowWith;
    }

    public void setWindowHeight(int windowHeight){
        this.windowHeight = windowHeight;
    }
    //getter

    public GrafikPoint getA(){
        return this.a;
    }

    public GrafikPoint getB(){
        return this.b;
    }

    public GrafikPoint getC(){
        return this.c;
    }

    public int getWindowWith(){
        return windowWith;
    }

    public int getWindowHeight(){
        return windowHeight;
    }

    @Override
    public void draw(Graphics2D g, Color color){
        g.drawLine(a.getX() + ((int)(offset/Controler.getScale())), ((int)(windowHeight/Controler.getScale())) - a.getY() - ((int)(offset/Controler.getScale())), b.getX() + ((int)(offset/Controler.getScale())), ((int)(windowHeight/Controler.getScale())) - b.getY() - ((int)(offset/Controler.getScale())));
        g.drawLine(b.getX() + ((int)(offset/Controler.getScale())), ((int)(windowHeight/Controler.getScale())) - b.getY() - ((int)(offset/Controler.getScale())), c.getX() + ((int)(offset/Controler.getScale())), ((int)(windowHeight/Controler.getScale())) - c.getY() - ((int)(offset/Controler.getScale())));
        g.drawLine(a.getX() + ((int)(offset/Controler.getScale())), ((int)(windowHeight/Controler.getScale())) - a.getY() - ((int)(offset/Controler.getScale())), c.getX() + ((int)(offset/Controler.getScale())), ((int)(windowHeight/Controler.getScale())) - c.getY() - ((int)(offset/Controler.getScale())));
        drawPixel(g, getCenter().getX() + ((int)(offset/Controler.getScale())), ((int)(windowHeight/Controler.getScale())) - getCenter().getY() - ((int)(offset/Controler.getScale())), Color.RED);
    }

    @Override
    public GrafikPoint getCenter(){
        GrafikPoint center;
        int x, y;

        x = (this.a.getX() + this.b.getX() + this.c.getX()) / 3;
        y = (this.a.getY() + this.b.getY() + this.c.getY()) / 3;

        center = new GrafikPoint(x, y);

        return center;

    }

    public GrafikPoint is_orthogonal(){
        if (a.getX() == b.getX() && a.getY() == c.getY()){
            return a;
        }
        if (a.getY() == b.getY() && a.getX() == c.getX()){
            return a;
        }
        if (c.getX() == b.getX() && a.getY() == c.getY()){
            return c;
        }
        if (c.getY() == b.getY() && a.getX() == c.getX()){
            return c;
        }
        if (a.getX() == b.getX() && b.getY() == c.getY()){
            return b;
        }
        if (a.getY() == b.getY() && b.getX() == c.getX()){
            return b;
        }
        return null;
    }
    
    public boolean checkEdges() {
        ArrayList<GrafikObject> objects = Controler.getInstance().getObjects();
        int gleichePunkte               = 0;
   
        if (objects.isEmpty()){
            if ((this.getA().getX() == 0 && this.getA().getY() == 0) ||
                (this.getB().getX() == 0 && this.getB().getY() == 0) ||
                (this.getC().getX() == 0 && this.getC().getY() == 0)){
                return true;
            }
            return false;
        }
        
        for (GrafikObject object : objects){
           
            
            if (object instanceof GrafikRectangle){
                GrafikRectangle obj = (GrafikRectangle) object;
                if (obj.getA().getX() == this.getA().getX() && obj.getA().getY() == this.getA().getY() ||
                    obj.getB().getX() == this.getA().getX() && obj.getB().getY() == this.getA().getY() ||
                    obj.getC().getX() == this.getA().getX() && obj.getC().getY() == this.getA().getY() ||
                    obj.getD().getX() == this.getA().getX() && obj.getD().getY() == this.getA().getY()){
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getB().getX() && obj.getA().getY() == this.getB().getY() ||
                    obj.getB().getX() == this.getB().getX() && obj.getB().getY() == this.getB().getY() ||
                    obj.getC().getX() == this.getB().getX() && obj.getC().getY() == this.getB().getY() ||
                    obj.getD().getX() == this.getB().getX() && obj.getD().getY() == this.getB().getY()){
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getC().getX() && obj.getA().getY() == this.getC().getY() ||
                    obj.getB().getX() == this.getC().getX() && obj.getB().getY() == this.getC().getY() ||
                    obj.getC().getX() == this.getC().getX() && obj.getC().getY() == this.getC().getY() ||
                    obj.getD().getX() == this.getC().getX() && obj.getD().getY() == this.getC().getY()){
                    gleichePunkte++;
                }     
            }
            
             if (object instanceof GrafikTriangle){
                GrafikTriangle obj = (GrafikTriangle) object;
                if (obj.getA().getX() == this.getA().getX() && obj.getA().getY() == this.getA().getY() ||
                    obj.getB().getX() == this.getA().getX() && obj.getB().getY() == this.getA().getY() ||
                    obj.getC().getX() == this.getA().getX() && obj.getC().getY() == this.getA().getY()){
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getB().getX() && obj.getA().getY() == this.getB().getY() ||
                    obj.getB().getX() == this.getB().getX() && obj.getB().getY() == this.getB().getY() ||
                    obj.getC().getX() == this.getB().getX() && obj.getC().getY() == this.getB().getY()){
                    gleichePunkte++;
                }
                if (obj.getA().getX() == this.getC().getX() && obj.getA().getY() == this.getC().getY() ||
                    obj.getB().getX() == this.getC().getX() && obj.getB().getY() == this.getC().getY() ||
                    obj.getC().getX() == this.getC().getX() && obj.getC().getY() == this.getC().getY()){
                    gleichePunkte++;
                } 
            }
            
             if (gleichePunkte >= 2){
                 return true;
             }
        }
        return false;
    }
}
